package plantshop.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.plaf.FontUIResource;

import plantshop.view.listeners.IPListener;
import plantshop.view.listeners.PlayerMouseListener;


public class PlayerMenu extends JPanel {
	private Map<String,BufferedImage> images;
	
	private static final long serialVersionUID = 1645657317518020036L;
	private final static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	private final static int frameWidth = screenSize.width;
   	private final static int frameHeight = screenSize.height;
   	private final static double xscale = frameWidth/1600.0;
   	private final static double yscale = frameHeight/900.0;
   	private static final int MAX_LENGTH = 12;
   
	private static final int[] IMPROPER_IP_POS = new int[]{(int)(450*xscale),(int)(750*yscale)};
	private static final int[] MALE_POS = new int[]{(int)(775*xscale),(int)(150*xscale)};
	private static final int[] FEMALE_POS = new int[]{(int)(325*xscale),(int)(150*xscale)};
	private static final Font STANDARD_FONT = new FontUIResource(new Font("Arial",0,38));
	private static final int CHARACTER_WIDTH = (int) (200*xscale);
	private static final int CHARACTER_HEIGHT = (int) (275*View.yscale);
	private static final int[] IP_POS = new int[]{(int)(450*xscale),(int)(715*yscale)};
	private static final int[] NAME_POS = new int[]{(int)(470*xscale),(int)(580*yscale)};
	private static final int NAME_WIDTH = (int)(500*xscale), NAME_HEIGHT = (int)(50*yscale);
	private static final int[] IP_TEXT_BOX = new int[]{(int)(450*xscale),(int)(675*yscale)};
	private static final int IP_TEXT_BOX_HEIGHT = (int)(50*yscale), IP_TEXT_BOX_WIDTH = (int)(500*xscale);
	
	private boolean multiplayer = false;
	private String IPAddress = "";
	private boolean ipSet = true;
	private boolean needsIP = false;
	private boolean needsName = false;
	private String playerName = "";

	public PlayerMenu(boolean multiplayer, JFrame frame,Map<String,BufferedImage> images, Map<String, BufferedImage[]> animations){
		this.setFocusable(true);
		this.multiplayer = multiplayer;
		this.addMouseListener(new PlayerMouseListener(this,frame,images, animations));
		if(multiplayer){
			this.addKeyListener(new IPListener(this));
		}
		this.images = images;
	}
	public void setNeedsIP(boolean b){
		needsIP = b;
	}
	public void setIPAddress(String IPAddress){
		this.IPAddress = IPAddress;
		this.ipSet = true;
	}
	public String getIPAddress(){
		return IPAddress;
	}
	
	@Override
	public void paint(Graphics g){
		//Draw Store
		g.drawImage(images.get("Store"), 0, 0, this);
		
		//Draw Menu
		g.drawImage(images.get("Player Menu"), 0, 0, this);
		g.setFont(STANDARD_FONT);
		
		//Draw Client Text
		if(multiplayer){
			g.drawImage(images.get("IPBox"),0,0,this);
			
			g.setColor(Color.BLACK);
			g.drawString(IPAddress, IP_POS[0], IP_POS[1]);
			
			if(needsIP){
				g.setColor(Color.RED);
				g.drawString("Set IP Before Selecting Character", IMPROPER_IP_POS[0],IMPROPER_IP_POS[1]);
			}
			
			//Draw Proper IP Text
			if(!ipSet){
				g.setColor(Color.RED);
				g.drawString("Invalid IP Address: Try Again!", IMPROPER_IP_POS[0],IMPROPER_IP_POS[1]+(int)(40*yscale));
			}
		}
		
		if(needsName){
			g.setFont(STANDARD_FONT);
			g.setColor(Color.RED);
			g.drawString("Please enter your name", (int)(500*xscale), (int)(550*yscale));
		}
		
		g.setColor(Color.BLACK);
		if(playerName!=null)
			g.drawString(playerName, NAME_POS[0]+(int)(10*xscale), NAME_POS[1]+(int)(43*yscale));
	}
	
	public int[] getMaleCoords(){
		return new int[]{MALE_POS[0],MALE_POS[1],CHARACTER_WIDTH,CHARACTER_HEIGHT};
	}
	
	public int[] getFemaleCoords(){
		return new int[]{FEMALE_POS[0],FEMALE_POS[1],CHARACTER_WIDTH,CHARACTER_HEIGHT};
	}
	
	public int[] getNameCoords(){
		return new int[]{NAME_POS[0],NAME_POS[1],NAME_WIDTH,NAME_HEIGHT};
	}
	
	public boolean isIP(){
		return ipSet;
	}
	
	public boolean isMultiplayer(){
		return multiplayer;
	}
	
	public void setNeedsName(boolean b){
		needsName = b;
	}
	
	public String getPlayerName(){
		return playerName;
	}
	
	public void removeLetter(){
		if(playerName.length() < 2){
			playerName = "";
		}
		else{
			playerName = playerName.substring(0, playerName.length()-1);
		}
	}
	public void checkIP(){
		String[] s = IPAddress.split("\\.");
		if(s.length != 4){
			ipSet = false;
		}
		String res = IPAddress.replaceAll("\\.", "");
		if(res.matches("\\D+")){
			ipSet = false;
		}
	}
	
	public void removeLetterFromIP(){
		if(IPAddress.length() < 2){
			IPAddress = "";
		}
		else{
			IPAddress = IPAddress.substring(0, IPAddress.length()-1);
		}
	}
	
	public void addLetterToIP(char c){
		IPAddress = IPAddress + c;
	}
	
	public void addLetterToName(char c){
		if(playerName.length() < MAX_LENGTH)
			playerName = playerName + c;
	}
	
	public int[] getIPCoords(){
		return new int[]{IP_TEXT_BOX[0],IP_TEXT_BOX[1],IP_TEXT_BOX_WIDTH,IP_TEXT_BOX_HEIGHT};
	}
}
